﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ReceManager : MonoBehaviour {
    public static ReceManager instance;
    public GameObject TeamPanel;
    public GameObject TeamItem;
    public GameObject AnswerPanel;
    public GameObject TeamAnswerItem;
    public GameObject PkBg;
    public GameObject RigisterBg;
    public UITexture res;
    public UILabel problem;
    public UILabel playName;
    public UITexture playHead;
    public GameObject sz;
    public GameObject CountDownBg;
    public UITexture DownTimeRes;
    public GameObject WinOrFailBg;
    public UITexture WOrFjz;
    public UILabel ZuanShiNum;
    public GameObject TipsPanel;
    public UITexture TipsRes;
    private int timer = 0;
    private int TipsTimer = 0;
    private string headIndex;
    private string id;
    private string rival_id;
    private List<string> mSurnameList;
    private List<string> mForenameList_Man;
    // Use this for initialization
    void Awake()
    {
        instance = this;
        InitList();
    }
    void Start () {
        SzActive();
        PlayerPrefs.SetInt( "PkNum", 0 );
        PlayerPrefs.SetInt( "PkTrue", 0 );
    }
	
	// Update is called once per frame
	void Update () {
		
	}
    //void OnGUI()
    //{
    //    if( GUI.Button( new Rect( 100, 100, 300, 300 ), "InitProblemAndAnswer" ) )
    //    {
    //        InitProblemAndAnswer();
    //    }
    //}
    void InitList()
    {
        mSurnameList = new List<string>{"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱",
            "秦", "尤", "许", "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘",
            "葛", "奚", "范", "彭", "郎", "鲁", "韦", "苗", "凤", "花", "方", "宁", "仇", "栾", "甘", "厉", "戎", "万俟", "司马", "上官", "欧阳", "夏侯", "诸葛", "闻人",
            "东方", "赫连", "皇甫", "尉迟", "公羊", "澹台", "公冶", "宗政", "濮阳", "淳于", "单于", "太叔", "申屠", "公孙", "仲孙", "轩辕", "令狐", "钟离", "宇文", "长孙",
            "慕容", "呼延", "归海", "端木", "左丘", "宗政", "司城", "司空", "司寇", "司鸿", "司马", "司徒", "司士", "落难", "寂寞", "流浪", "奔跑", "帅帅", "傲娇", "骄傲",
            "卖萌", "风流", "隐者", "神将", "天尊", "罗刹", "默默", "风云", "拽拽", "叨叨", "淡淡", "无聊", "酷酷", "可爱", "女王", "萝莉", "正太", "大叔", "御姐", "呱呱",
            "乖乖", "怪怪", "归来", "飘逸", "胭脂", "小小", "大大", "笑笑", "萧萧", "凹凸", "轩轩", "萱萱", "喵喵", "汪汪", "旺旺", "姗姗", "山山", "淡定", "风风", "疯疯",
            "圆圆", "方方", "冷冷", "等等", "晴天", "嬉游", "默默", "晨晨", "凌霄", "凌霄", "凌霄", "子夜", "琉璃", "云裳", "皇", "第一", "第二",
            "第三", "归海", "星瞳", "老板", "绿茶", "苹果", "雪梨", "疯疯", "夜雨", "低调", "安静", "倾世", "倾城", "昆仑", "蓬莱", "玄都", "萌", "奇奇", "芊芊", "浅浅",
            "淡定", "君子", "北方", "小北", "神殇", "天师", "流落", "修罗", "柠檬", "猪头", "虔诚", "低语", "夏至", "轻狂", "勇敢", "年少", "蔡", "郁", "洪", "乌", "计",
            "温", "麦麦", "东东", "阳阳", "霸道", "亮亮", "明明", "温暖", "冷眼", "欢乐", "轩辕", "离别", "猪猪", "猴头", "呆子", "上仙", "大神", "菜鸟", "水货", "搞笑",
            "悲伤", "高手", "少年", "侠者", "富贵", "帅气", "沧桑", "忧伤", "文", "狂傲", "疯狂", "美女", "买醉", "坏坏", "微笑", "熙", "红尘", "霍", "宁静", "吵闹", "荭人",
            "裴", "栾", "穆", "尚", "耿", "彭", "燕", "瞿", "荣", "谭", "申", "北方", "山人", "常", "余", "纪", "唠叨", "轻萧", "尹", "贝", "乔", "西门", "文", "蒋", "武",
            "席", "盛", "祝", "景", "洪", "邢", "关", "甄", "辛", "章", "温", "祁", "齐", "岳", "欧", "卫", "东", "信", "冷", "车", "杭", "郦", "师", "帅", "荆", "乐正",
            "拓拔", "谷粱", "赫连"};
        mForenameList_Man = new List<string>{"从寒", "慕青", "从安", "冰枫", "元风", "忆山", "以寒", "寒凡", "冷安", "采文", "乐之", "笑白",
            "之山", "如天", "沛文", "访风", "天亦", "乐松", "涵山", "恨寒", "问枫", "以柳", "辰逸", "浩宇", "瑾瑜", "皓轩", "擎苍", "擎宇", "致远", "烨磊", "晟睿", "文博",
            "天佑", "英杰", "致远", "俊驰", "雨泽", "烨磊", "伟奇", "文昊", "修洁", "黎昕", "旭尧", "圣杰", "俊楠", "鸿涛", "伟祺", "荣轩", "越泽", "浩宇", "瑾瑜", "皓轩",
            "志泽", "子轩", "睿渊", "弘文", "哲瀚", "雨泽", "楷瑞", "建辉", "晋鹏", "天磊", "绍辉", "浩南", "泽洋", "鑫磊", "鹏煊", "博文", "昊强", "越泽", "旭尧", "伟宸",
            "志泽", "博超", "君浩", "子骞", "鹏涛", "炎彬", "鹤轩", "伟泽", "越彬", "风华", "靖琪", "明辉", "伟诚", "明轩", "绍辉", "健柏", "英杰", "修杰", "弘文", "峻熙",
            "嘉懿", "煜城", "君轩", "烨伟", "苑博", "鹏涛", "炎彬", "鹤轩", "伟泽", "君昊", "熠彤", "鸿煊", "博涛", "苑杰", "黎昕", "烨霖", "烨华", "煜祺", "智宸", "正豪",
            "昊然", "明杰", "立诚", "立轩", "立辉", "峻熙", "嘉懿", "懿轩", "明杰", "荭人", "苑博", "鹏涛", "炎彬", "鹤轩", "伟泽", "君昊", "熠彤", "鸿煊", "博涛", "苑杰",
            "黎昕", "烨霖", "哲瀚", "雨泽", "楷瑞", "建辉", "雨泽", "烨磊", "国豪", "黎昕", "旭尧", "圣杰", "鑫鹏", "俊楠", "鸿涛", "伟祺", "荣轩", "浩宇", "晋鹏", "瑾瑜",
            "昊天", "思聪", "展鹏", "笑愚", "志强", "炫明", "雪松", "思源", "智渊", "思淼", "晓啸", "天宇", "浩然", "文轩", "鹭洋", "振家", "乐驹", "晓博", "文博", "昊焱",
            "立果", "金鑫", "锦程", "嘉熙", "鹏飞", "子默", "思远", "浩轩", "语堂", "聪健", "可乐", "雪碧", "迟景", "北至", "冷仓", "云烁", "青瞳", "妖夜", "依海", "不群",
            "彦锐", "枫落", "毅哲", "毅哲", "笑雨", "康昊", "思卿", "黎夜", "以陌", "子乔", "小贤", "神奇", "夜然", "清扬", "林畅", "白夜", "莫易", "凌澈", "凌洛", "清扬",
            "清一", "亦凌", "霄寒", "漠谦", "唐果", "临渊", "千赫", "律泽", "羽凉", "子诺", "少瞳", "雨恒", "凯伦", "子瑜", "寒杰", "莫宏", "远洛", "小洛", "小凡", "阳阳",
            "洋洋", "畅", "洛", "澈", "翎", "杰", "智", "寒", "综畅", "少商", "清", "嘉", "骄", "漠", "谦", "非墨", "天畅", "睿", "卿", "炎", "楚", "吟", "天寒", "洛翊",
            "黎夜", "彦锐", "朔", "羽凉", "白辰", "景天", "逸", "宇", "影", "伦", "泽", "瞳", "落", "瑞", "霖", "逸熙", "沐", "寒染", "墨皓", "皓", "晴", "宏", "冷军",
            "云烁", "艺轩", "轩", "倚哲", "少齐", "夜", "铭", "渊", "野", "安邦", "安翔", "彬郁", "简", "才哲", "承泽", "德海", "才艺", "博艺", "博远", "博远", "波鸿",
            "博明", "成济", "德曜", "德佑", "德庸", "高翰", "高轩", "光熙", "浩歌", "弘盛", "宏浚", "鸿博", "鸿哲", "华藏", "华皓", "华翰", "宏恺", "涵涵", "涵煦", "光启",
            "冠玉", "风华", "飞文", "嘉佑", "景同", "俊茂", "俊语", "俊喆", "景明", "建明", "嘉致", "康德", "凯乐", "立轩", "明喆", "浦泽", "鹏天", "鹏海"};
    }
    public void RandomNameClick()
    {
        int surnameIndex = Random.Range(0, mSurnameList.Count);
        int nameIndex = Random.Range(0, mForenameList_Man.Count);
        headIndex = Random.Range( 2, InitConfig.instance.RankConfigDictionary.Count ).ToString();
        playName.text = mSurnameList[surnameIndex] + mForenameList_Man[nameIndex];
        playHead.mainTexture = chengyuManager.TextAssetToTexture( "chengyu/headRes/" + InitConfig.instance.RankConfigDictionary[headIndex].headRes );
        if( playName.text == string.Empty )
            return;
        if( headIndex == string.Empty )
            return;
        PlayerPrefs.SetString( "playName", playName.text );
        PlayerPrefs.SetString( "playHead", headIndex );        
    }
    //SZ state
    public void SzActive()
    {
        string playname = PlayerPrefs.GetString( "playName", "PlayNameNull" );
        if( playname == "PlayNameNull" )
        {
            //初始化一个名字和头像
            RandomNameClick();
            sz.SetActive( true );
        }
        else
        {
            sz.SetActive( false );
            playName.text = PlayerPrefs.GetString( "playName" );
            Debug.Log( "|||||||||||||" + PlayerPrefs.GetString( "playHead" ) );
            playHead.mainTexture = chengyuManager.TextAssetToTexture( "chengyu/headRes/" + PlayerPrefs.GetString( "playHead" ) );

        }
        
    }  
    //开始pk
    public void StartPK()
    {       
        
        RigisterBg.SetActive(false);
        PkBg.SetActive(true);
        
        //倒计时面板
        DownTimerClick();
    }
   

    public void DownTimerClick()
    {
        timer = 4;
        CountDownBg.SetActive( true );
        DownTimeRes.mainTexture = chengyuManager.TextAssetToTexture( "cheng/time/" + 3 );           
        InvokeRepeating( "Time_count", 0.5f, 1.0f );
    }
    void Time_count()
    {
        if( timer > 0 )
        {
            timer--;           
            DownTimeRes.mainTexture = chengyuManager.TextAssetToTexture( "chengyu/time/" + timer.ToString() );
        }
        else
        {
            CancelInvoke();
            CountDownBg.SetActive( false );
            //倒计时结束，初始化 Ieam ，problem,Answer
            //随机对手id i=0时为对手
            rival_id = Random.Range( 1, 49 ).ToString();
            InitTeam();
            InitProblemAndAnswer();

        }
    }
    public void InitTeam()
    {
        foreach (Transform child in TeamPanel.transform)
        {
            Destroy(child.gameObject);
        }      
        for (int i = 0; i < 2; i++)
        {
            GameObject go = Instantiate(TeamItem);
            go.transform.parent = TeamPanel.transform;
            go.transform.localPosition = new Vector3(-230+i*450, 450, 0);
            go.transform.localScale = new Vector3(1, 1);
            go.name = "TeamItem" + i;
            if (i == 0)
            {
                go.GetComponent<UITexture>().mainTexture = chengyuManager.TextAssetToTexture("chengyu/headRes/" + rival_id);
                go.transform.Find("Name").GetComponent<UILabel>().text = InitConfig.instance.RankConfigDictionary[rival_id].name;
            }
            else
            {
                go.GetComponent<UITexture>().mainTexture = chengyuManager.TextAssetToTexture( "chengyu/headRes/" + PlayerPrefs.GetString( "playHead" ) );
                go.transform.Find("Name").GetComponent<UILabel>().text = PlayerPrefs.GetString("playName");
            }
        }    

    }
  
    public void InitProblemAndAnswer()
    {
        foreach (Transform child in AnswerPanel.transform)
        {
            Destroy(child.gameObject);
        }
        //随机问题id
         id = Random.Range( 1, 73 ).ToString();
        //问题是图片
        // res.mainTexture = chengyuManager.TextAssetToTexture( "chengyu/res/" + chengyuManager.Instance.configDictionary[id].res );
        //问题是文字
        problem.text = InitConfig.instance.ReceConfigDictionary[id].problem;
        for( int i = 0; i < InitConfig.instance.ReceConfigDictionary[id].disturb.Length; i++ )
        {
            GameObject go = Instantiate( TeamAnswerItem );
            go.transform.parent = AnswerPanel.transform;
            go.transform.localPosition = new Vector3( 0, -169 - i * 105 );
            go.transform.localScale = new Vector3( 1, 1 );
            go.name = "TeamAnswerItem" + i;
            //设置选项
            string disturb = string.Empty;
            if (i == 0)
            {
                disturb = "A:";
            }
            else if (i == 1)
            {
                disturb = "B:";
            }
            else if (i == 2)
            {
                disturb = "C:";
            }
            else if (i == 3)
            {
                disturb = "D:";
            }
            go.transform.Find("AnswerLabel").GetComponent<UILabel>().text =disturb+ InitConfig.instance.ReceConfigDictionary[id].disturb[i];
           
        }
    }
    public void VerifyAnswer(string answer)
    {
        if( answer == InitConfig.instance.ReceConfigDictionary[id].answer )
        {
            Debug.Log( "answer is true" );            
            PlayerPrefs.SetInt( "PkNum", PlayerPrefs.GetInt( "PkNum" ) + 1 );
            PlayerPrefs.SetInt( "PkTrue", PlayerPrefs.GetInt( "PkTrue" ) + 1 );

            if( PlayerPrefs.GetInt( "PkNum" ) >= 5 )
            {
                if( PlayerPrefs.GetInt( "PkTrue" ) == 5 )
                {
                    Debug.Log( "胜利一轮!" );
                    //
                    ZuanShiNum.text = (100 + PlayerPrefs.GetInt("PkTrue") * 20).ToString();
                    WOrFjz.mainTexture = chengyuManager.TextAssetToTexture( "chengyu/time/" + "huangjz" );
                    WinOrFailBg.SetActive(true);
                }
                else
                {
                    Debug.Log( "输了一轮！" );
                    //PK失败面板
                    ZuanShiNum.text = (100 + PlayerPrefs.GetInt("PkTrue") * 20).ToString();
                    WOrFjz.mainTexture = chengyuManager.TextAssetToTexture( "chengyu/time/" + "heijz" );
                    WinOrFailBg.SetActive(true);
                }

            }
            else
            {
                Tips( "win" );

            }

        }
        else
        {
            Debug.Log( "answer is error" );
            PlayerPrefs.SetInt( "PkNum", PlayerPrefs.GetInt( "PkNum" ) + 1 );
            
            if( PlayerPrefs.GetInt( "PkNum" ) >= 5 )
            {
                Debug.Log( "输了一轮！" );
                //PK失败面板
                WOrFjz.mainTexture = chengyuManager.TextAssetToTexture( "chengyu/time/" + "heijz" );
                ZuanShiNum.text = (100 + PlayerPrefs.GetInt("PkTrue") * 20).ToString();
                WinOrFailBg.SetActive( true );
            }
            Tips("fail");
        }
        
    }
    //继续PK
    public void KeepPK()
    {
        DownTimerClick();
        WinOrFailBg.SetActive(false);
        PlayerPrefs.SetInt("coin", PlayerPrefs.GetInt("coin", 0) + int.Parse(ZuanShiNum.text));
        AudioManager.instance.AddCoinOnclick();
        PlayerPrefs.SetInt( "PkNum", 0 );
        PlayerPrefs.SetInt("PkTrue", 0);
    }  
    public void ExitPK()
    {
        WinOrFailBg.SetActive(false);
        //整个功能界面关闭
        RigisterBg.SetActive( true );
        PkBg.SetActive( false );
        PlayerPrefs.SetInt("coin", PlayerPrefs.GetInt("coin", 0) + int.Parse(ZuanShiNum.text));
        AudioManager.instance.AddCoinOnclick();
        MainPanelManager.instance.ReceExit();
        PlayerPrefs.SetInt( "PkNum", 0 );
        PlayerPrefs.SetInt("PkTrue", 0);
    }
    //胜利失败弹屏
    public void Tips(string data)
    {
        TipsTimer = 2;
        //根据data显示
        TipsPanel.SetActive(true);
        TipsRes.mainTexture = chengyuManager.TextAssetToTexture("chengyu/time/" + data);
        InvokeRepeating("Inoke_Tips", 0.5f, 1.0f );
    }
    public void Inoke_Tips()
    {
        if( TipsTimer > 0 )
        {
            TipsTimer--;
        }
        else
        {
            CancelInvoke();
            TipsPanel.SetActive(false);
            //Tips结束，刷新 Ieam ，problem,Answer
            InitTeam();
            InitProblemAndAnswer();
        }
    }

}
